package org.hxyjs.dao.impl;

import org.hxyjs.dao.MemberTypeDao;
import org.hxyjs.entity.MemberType;
import org.hxyjs.mapper.MemberTypeMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("memberTypeDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class MemberTypeDaoImpl implements MemberTypeDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private MemberTypeMapper memberTypeMapper;

    public List<MemberType> getAllMemberTypes() throws Exception {
        return memberTypeMapper.selectByExample(null);
    }

    @Override
    public MemberType getMemberTypeByUno(String mtno) throws Exception {
        return memberTypeMapper.selectByPrimaryKey(mtno);
    }

    @Override
    public List<MemberType> getMemberTypesByPage(PageInfo pageInfo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) memberTypeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return memberTypeMapper.getMemberTypesByPage(map);
    }

    @Override
    public Integer deleteMemberType(String mtno) {
        return memberTypeMapper.deleteByPrimaryKey(mtno);
    }

    @Override
    public Integer updateMemberType(MemberType memberType) {
        return memberTypeMapper.updateByPrimaryKey(memberType);
    }



    @Override
    public MemberType getMemberTypesWithOrders(String mtno) throws Exception {
        return null;
    }

    @Override
    public MemberType getAccountByAccountName(String mtno) throws Exception {
        return null;
    }

    @Override
    public Integer addMemberTypeByUno(MemberType memberType){
        memberType.setMtno(initId());
        return memberTypeMapper.insertSelective(memberType);
    }
//
//    @Override
//    public MemberType getMemberTypesWithOrders(String mtno) throws Exception {
//        return memberTypeMapper.getMemberTypesWithOrders(mtno);
//    }
//    @Override
//    public MemberType getAccountByAccountName(String uname) throws Exception {
//        return memberTypeMapper.getAccountByAccountName(uname);
//    }
//
//

    public String initId() {
        String maxMtno= memberTypeMapper.getMaxMtno();
        return org.hxyjs.utils.GenNumTools.initId("m", 4, maxMtno);
    }
}
